package com.limeng.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.limeng.sys.entity.User;
import com.limeng.sys.entity.UserRole;
import com.limeng.sys.mapper.UserMapper;
import com.limeng.sys.mapper.UserRoleMapper;
import com.limeng.sys.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.limeng.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author limeng
 * @since 2023-03-27
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 登录方法
     * @param user
     * @return
     */
    @Override
    public Map<String, Object> login(User user) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,user.getUsername());
        wrapper.eq(User::getPassword,user.getPassword());
        User loginUser = this.baseMapper.selectOne(wrapper);
        if (loginUser != null){
            loginUser.setPassword(null);
            //创建jwt
            String token = jwtUtil.createToken(loginUser);
            //返回数据
            Map<String,Object> data = new HashMap<>();
            data.put("token",token);
            return data;
        }
        return null;
    }

    @Override
    public Map<String, Object> getUserInfo(String token) {
        //获取用户信息
        User loginUser = null;
        try {
            loginUser = jwtUtil.parseToken(token,User.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        if(loginUser != null){
            HashMap<String, Object> map = new HashMap<>();
            map.put("username",loginUser.getUsername());

            return map;
        }

        return null;
    }

    @Override
    public void logout() {

    }

    /**
     * 新增用户及其角色
     * @param user
     */
    @Override
    public void addUser(User user) {
        //写入用户表
        this.baseMapper.insert(user);
        //写入角色表
        List<Integer> roleIdList = user.getRoleIdList();
        if (roleIdList != null){
            for (Integer roleId : roleIdList) {
                userRoleMapper.insert(new UserRole(null,user.getId(),roleId));
            }
        }
    }

    /**
     * 修改用户及其角色
     * @param user
     */
    @Override
    public void updateUserById(User user) {
        //更新用户表
        this.baseMapper.updateById(user);
        //删除角色表
        LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRole::getUserId,user.getId());
        userRoleMapper.delete(wrapper);
        //新增角色表
        List<Integer> roleIdList = user.getRoleIdList();
        if (roleIdList != null){
            for (Integer roleId : roleIdList) {
                userRoleMapper.insert(new UserRole(null,user.getId(),roleId));
            }
        }
    }

    /**
     * 删除用户及其角色
     * @param id
     */
    @Override
    public void deleteUserById(Integer id) {
        //删除用户
        this.baseMapper.deleteById(id);
        //删除角色
        LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRole::getUserId,id);
        userRoleMapper.delete(wrapper);
    }

    @Override
    public User getUserById(Integer id) {
        //查询用户
        User user = this.baseMapper.selectById(id);
        //查询角色列表
        LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRole::getUserId,id);
        List<UserRole> userRolesList = userRoleMapper.selectList(wrapper);


        List<Integer> roleIdList = userRolesList.stream()
                .map(userRole -> {return userRole.getRoleId();})
                .collect(Collectors.toList());

        user.setRoleIdList(roleIdList);
        return user;
    }
}
